<template>
	<view>
		<!-- 搜索区域 -->
		<!-- native:给组件(根元素)最外层元素  注册事件 -->
		<my-search @click.native="gotoSearch"></my-search>

		<!-- 分类 -->
		<view class="scroll-view-container">
			<!-- 一级分类 -->
			<scroll-view scroll-y class="left-scroll-view" :style="{height:wh+'px'}">
				<view class="left-scroll-view-item" v-for="(item,i) in cateList" :key="i" :class="{active:i===active}"
					@click="changeActive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 二级、三级分类 -->
			<scroll-view scroll-y class="right-scroll-view" :style="{height:wh+'px'}" :scroll-top='scrollTop'>
				<view class="cate-lv2" v-for="(item2,i2) in cateLevel2List" :key="i2">
					<view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
					<view class="cate-lv3-list">
						<navigator :url="`/subpkg/goods_list/goods_list?id=${item3.cat_id}`" class="cate-lv3-item"
							v-for="(item3,i3) in item2.children" :key="i3">
							<image :src="item3.cat_icon" mode="">{{item3.cat_name}}</image>
						</navigator>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import tabbarMinxin from '../../mixins/tabbar.js'
	export default {
		mixins: [tabbarMinxin],
		data() {
			return {
				cateList: [], // 一级分类
				wh: 0,
				active: 0,
				cateLevel2List: [], // 二级分类
				scrollTop: 0, // 右侧滚动条位置
			};
		},
		onLoad() {
			// 获取设备信息
			const sysInfo = uni.getSystemInfoSync()
			// console.log(sysInfo.windowHeight);
			this.wh = sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods: {
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			async getCateList() {
				const {
					data
				} = await uni.$http.get('/categories')
				if (data.meta.status !== 200) return uni.$showToast()
				this.cateList = data.message // 一级分类
				this.cateLevel2List = this.cateList[this.active].children
			},
			changeActive(i) {
				// 切换一级高亮导航
				this.active = i

				// 获取二级分类数据
				this.cateLevel2List = this.cateList[i].children

				this.scrollTop = this.scrollTop ? 0 : 1
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}
	}

	/* 二级分类 */
	.cate-lv2-title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	/* 三级分类 */
	.cate-lv3-list {
		display: flex;
		flex-wrap: wrap;

		.cate-lv3-item {
			width: 33.33%;
			margin-bottom: 10px;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 60px;
				height: 60px;
			}

			text {
				font-size: 12px;
			}
		}
	}
</style>